﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using WareHouseManagement.Common;

namespace WareHouseManagement.Commom
{
    public static class EnumDefination
    {

        public static ObservableCollection<Val_Des> MaterialTypeList { get; set; } = GetVal_Dess<MaterialType>();

        public static ObservableCollection<Val_Des> GetVal_Dess<T>()
        {
            ObservableCollection<Val_Des> EnumModelList = new ObservableCollection<Val_Des>();
            Array array = Enum.GetValues(typeof(T));
            foreach (var item in array)
            {//通过遍历枚举拿到遍历的枚举值，然后根据枚举值拿到对应的枚举名称 在添加到list集合里 就Ok 很实在的方法 而且还很简单
                Val_Des EnumModel = new Val_Des();
                EnumModel.ID = Convert.ToInt32(item);
                EnumModel.Val = Convert.ToInt32(item);
                EnumModel.Description = GetEnumDescription((T)item);
                if (EnumModel.Description == ((T)item).ToString()) continue;
                EnumModelList.Add(EnumModel);
            }
            return EnumModelList;
        }
        public static string GetEnumDescription<T>(T enumValue)
        {
            string value = enumValue.ToString();
            FieldInfo field = enumValue.GetType().GetField(value);
            object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);  //获取描述属性
            if (objs == null || objs.Length == 0)  //当描述属性没有时，直接返回名称
                return value;
            DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
            return descriptionAttribute.Description;
        }
        public enum MaterialType
        {
            [Description("空")]
            Empty = 1,
            [Description("空架子")]
            EmptyShelf,
            [Description("满料")]
            Full
        }

    }

}
